#include "Stdafx.h"
#include "Direct3DSurface9.h"

using namespace Vortex::Wrapper::Direct3D;

Direct3DSurface9::Direct3DSurface9(IDirect3DSurface9* handle) : _handle(handle) {	
	D3DSURFACE_DESC d3ddesc;
	_handle->GetDesc(&d3ddesc);
	_description = gcnew SurfaceDesc(&d3ddesc);
}

Direct3DSurface9::~Direct3DSurface9() {
	_handle->Release();
}

HResult Direct3DSurface9::LockRect(System::Drawing::Rectangle rectToLock, D3DLock lockFlags, D3DLockedRect% lockedRect) {
	RECT rect = { rectToLock.Left, rectToLock.Top, rectToLock.Right, rectToLock.Bottom };
	D3DLOCKED_RECT lRect;
	
	HResult result = HResult(_handle->LockRect(&lRect, &rect, (DWORD)lockFlags));
	lockedRect.pBits = lRect.pBits;
	lockedRect.Pitch = lRect.Pitch;
	return result;
}

HResult Direct3DSurface9::UnlockRect() {
	return HResult(_handle->UnlockRect());
}


HResult Direct3DSurface9::GetDC(IntPtr% hDC) {
	HDC hdc;
	HResult result = HResult(_handle->GetDC(&hdc));
	hDC = IntPtr(hdc);
	return result;
}

HResult Direct3DSurface9::ReleaseDC(IntPtr hDC) {
	return HResult(_handle->ReleaseDC((HDC)hDC.ToPointer()));
}